package com.wtwd.device.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.AppFrequencyMapper;
import com.wtwd.device.model.entity.Frequency;
import com.wtwd.device.service.AppDeviceService;
import com.wtwd.device.service.AppIFrequencyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-21
 */
@Service
public class AppFrequencyServiceImpl extends ServiceImpl<AppFrequencyMapper, Frequency> implements AppIFrequencyService {


    @Resource
    private AppFrequencyMapper frequencyMapper;

    @Resource
    private AppDeviceService deviceService;


    @Override
    public boolean insertOrUpdate(Frequency frequency) {
        if (frequency == null) {
            return false;
        }
        return frequencyMapper.insertOrUpdate(frequency) > 0 ? true : false;
    }

    @Override
    public boolean updateState(Long deviceId, int type, String number, int state) {
        return frequencyMapper.updateState(deviceId, type, number, state) > 0 ? true : false;
    }

    @Override
    public Frequency queryFrequencyByImeiAndType(String imei, Integer type) {
        Long deviceId = deviceService.getDeviceIdByImei(imei);
        if (deviceId == null) {
            return null;
        }
        return new LambdaQueryChainWrapper<>(frequencyMapper)
                .eq(Frequency::getDeviceId, deviceId)
                .eq(Frequency::getType, type)
                .one();
    }
}
